package com.ys.controller;

import com.ys.mapper.BumenMapper;
import com.ys.pojo.Administrators;
import com.ys.pojo.Position;
import com.ys.service.AdministratorsService;
import com.ys.service.PositionService;
import com.ys.util.Duanxin;
import com.ys.util.alb.SendSms;
import com.ys.vo.PageVo;
import com.ys.vo.ResultVo;
import com.ys.vo.UploadVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class UsersContorller {
    @Resource
    private AdministratorsService administratorsService;
    @Resource
    private PositionService positionService;

    //个人页面
    @RequestMapping("/users/user")
    public String users(){
        return "/users/user";
    }
    //管理员页面
    @RequestMapping("/users/userse")
    public String userse(){
        return "/users/userse";
    }
    //角色页面
   @RequestMapping("/users/ueerType")
    public String ueerType(){
        return "/users/ueerType";
    }

    /**
     * 菜单
     * @param session
     * @return
     */
    @RequestMapping("/users/atcurrentUser")
    @ResponseBody
    public Administrators atcurrentUser(HttpSession session){
        //通过sesstion取到当前用户的账号和密码
        Administrators administrators = (Administrators) session.getAttribute("objs");
        //再一次的进行用户链表查询
        Administrators administrators1 = administratorsService.login(administrators.getAccount());
        Position position =  positionService.objPosition(administrators.getPositionidss());
        //查出来的东西放在administrators1中去
        administrators1.setPosi(position.getPosi());
        return administrators1;
    }

    /**
     * 上传图片
     * @param attach
     * @param request
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/user/uplaod", produces = "html/text;charset=utf-8")
    @ResponseBody
    public ResultVo uplaod(@RequestParam("attach") MultipartFile attach, HttpServletRequest request,HttpSession session) throws IOException {
        //当前的id
        Administrators administrators = (Administrators) session.getAttribute("objs");
        //获取当前照片的id
        String urlName = UploadVo.upload(attach,request);
        System.out.println(urlName+"---"+administrators.getId());

        if(administratorsService.updateUplaod(urlName,administrators.getId())>0){
            return ResultVo.success("上传成功!");
        }else{
            return ResultVo.success("上传失败!");
        }
    }

    /**
     * 查询所有的用户
     * @param administrators
     * @return
     */
    @RequestMapping("/users/userAll")
    @ResponseBody
    public ResultVo userAll(Administrators administrators){
        ResultVo resultVo = new ResultVo();
        resultVo.setObj(administratorsService.finalByAll(administrators));
        return resultVo;
    }

    /**
     * 用户的类型查询（职位）
     * @return
     */
    @RequestMapping("/users/userType")
    @ResponseBody
    public ResultVo userType(){
        ResultVo resultVo = new ResultVo();
        resultVo.setObj(positionService.selectAll());
        return resultVo;
    }

    /**
     * 添加用户
     * @param administrators
     * @return
     */
    @RequestMapping(value = "/users/insert", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo insert(@RequestBody Administrators administrators){
//        System.out.println(administrators);
        administrators.setPwdshiro(UUID.randomUUID().toString());
        String jia = new Md5Hash(administrators.getPassword(),administrators.getPwdshiro()).toString().replace("-","");
        administrators.setPassword(jia);
        ResultVo resultVo = new ResultVo();
        if(administratorsService.addsInsert(administrators)){
            resultVo.setMsg("添加成功!");
        }else{
            resultVo.setMsg("添加失败!");
        }
        return resultVo;
    }

    /**
     * 修改岗位
     * @param id
     * @param ids
     * @return
     */
    @RequestMapping(value = "/users/updateGangwei", produces = "html/text;charset=utf-8")
    @ResponseBody
    public ResultVo updateGangwei(int id,int ids){
        ResultVo resultVo = new ResultVo();
        if(id==1){
            if(administratorsService.updateGangwei(id,ids)){
                resultVo.setMsg("在岗修改成功!");
            }else{
                resultVo.setMsg("在岗修改失败!");
            }
        }else{
            if(administratorsService.updateGangwei(id,ids)){
                resultVo.setMsg("停职修改成功!");
            }else{
                resultVo.setMsg("停职修改失败!");
            }
        }
        return resultVo;
    }

    /**
     * 修改用户
     * @param ad
     * @return
     */
    @RequestMapping(value = "/user/updsUpdateUser", produces = "html/text;charset=utf-8")
    @ResponseBody
    public ResultVo updsUpdateUsee(@RequestBody Administrators ad){
        ResultVo resultVo = new ResultVo();
        if(administratorsService.updsupdataUser(ad)>0){
            resultVo.setMsg("修改成功!");
        }else{
            resultVo.setMsg("修改失败!");
        }
        return resultVo;
    }

    /**
     * 正常的修改密码
     * @param ypwd
     * @param pwdx
     * @return
     */
    @RequestMapping(value = "/user/updatepwd", produces = "html/text;charset=utf-8")
    @ResponseBody
    public ResultVo updatepwd(String ypwd,String pwdx){
        ResultVo resultVo = new ResultVo();
        Session session = SecurityUtils.getSubject().getSession();
        Administrators ad = (Administrators)session.getAttribute("objs");
        String namePwd = new Md5Hash(ypwd,ad.getPwdshiro()).toString().replace("-","");
        if(!namePwd.equals(ad.getPassword())){
            resultVo.setMsg("密码错误!修改失败!");
            return resultVo;
        }
        String UUid = UUID.randomUUID().toString();
        String jia = new Md5Hash(pwdx,UUid).toString().replace("-","");
        if(administratorsService.updatePwd(jia,UUid,ad.getId())){
            resultVo.setMsg("1");
        }else{
            resultVo.setMsg("修改失败!");
        }
        return resultVo;
    }

    /**
     * 手机号修改密码
     * @param m
     * @param suijiNumber
     * @return
     */
    @RequestMapping(value = "/user/updatespwd", produces = "html/text;charset=utf-8")
    @ResponseBody
    public ResultVo updatespwd(String m,String suijiNumber,String shoujihao){
        ResultVo resultVo = new ResultVo();
        Session session = SecurityUtils.getSubject().getSession();
        Administrators ad = (Administrators) session.getAttribute("objs");
        //发送短信suijiNumber
        String uuid = UUID.randomUUID().toString();
        String mima = new Md5Hash(m,uuid).toString().replace("_","");

        if(administratorsService.updatePwd(mima,uuid,ad.getId())){
            resultVo.setMsg("修改成功!请重新登录!");
        }else{
            resultVo.setMsg("修改失败!");
        }
        return resultVo;
    }


    @RequestMapping(value = "/user/yanzhenma", produces = "html/text;charset=utf-8")
    @ResponseBody
    public ResultVo yanzhenma(String suijiNumber,String shoujihao,String as){
        ResultVo resultVo = new ResultVo();
        System.out.println(suijiNumber+"---"+shoujihao);
        Map<String,String> map = new HashMap();
        SendSms sendSms = new SendSms();
        map.put("mobile",shoujihao);
        map.put("code",suijiNumber);
        map.put("yx",as);
        sendSms.send(map);

//        Duanxin duanxin = new Duanxin();
//        duanxin.duanXin(shoujihao,suijiNumber);
        resultVo.setMsg("ok");
        return resultVo;
    }


    @Resource
    private BumenMapper bumenMapper;
        /**
         * 用户的类型
         * @return
         */
        @RequestMapping("/users/bumenMapper")
        @ResponseBody
        public ResultVo bumenMapper(){
            ResultVo resultVo = new ResultVo();
            resultVo.setObj(bumenMapper.select());
            return resultVo;
        }


    /**
     * 添加部门
     * @return
     */
    @RequestMapping(value = "/users/bumen", produces = "html/text;charset=utf-8")
    @ResponseBody
    public ResultVo bumen(String names){
        ResultVo resultVo = new ResultVo();
        if(bumenMapper.insert(names)){
            resultVo.setMsg("添加成功!");
        }else{
            resultVo.setObj("添加失败!");
        }
        return resultVo;
    }
    /**
     *查询角色
     * @return
     */
    @RequestMapping("/users/ueerTypes")
    @ResponseBody
    public ResultVo ueerTypes(){
        ResultVo resultVo = new ResultVo();
        resultVo.setObj(positionService.selectAll());
        return resultVo;
    }

    /**
     *添加角色
     * @return
     */
    @RequestMapping(value = "/users/ueerTypesintser", produces = "html/text;charset=utf-8")
    @ResponseBody
    public ResultVo ueerTypesintser(String name){
        ResultVo resultVo = new ResultVo();
        if(positionService.insert(name)){
            resultVo.setMsg("添加角色成功!");
        }else{
            resultVo.setMsg("添加角色失败!");
        }
        return resultVo;
    }

}
